/*
链栈的实现
初始化     InitStack();
入/压/进栈 Push();
出栈 /弹栈 Pop();
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct StackNode
{
    int data;
    struct StackNode *next;
}StackNode;
typedef StackNode *LinkStackPtr;

typedef struct
{
    LinkStackPtr top;
    int count;
} LinkStack;

void visit(int data)
{
    printf("%d ", data);
}

void printstack(LinkStack stack)
{
    LinkStackPtr p;
    p = stack.top;
    while (p != NULL)
    {
        visit(p->data);
        p = p->next;
    }
    putchar('\n');
}

void InitStack(LinkStack *stack)
{
    printf("init linkstack -> %p\n", stack);
    stack->top = NULL;
    stack->count = 0;
    // stack->top = (LinkStackPtr) malloc(sizeof(struct StackNode));
    /* if (stack->top == NULL)
    {
        exit(-1);
    } */
}

void Push(LinkStack *stack, int data)
{
    LinkStackPtr tmp = (LinkStackPtr) malloc(sizeof(struct StackNode));
    tmp->next = stack->top;
    tmp->data = data;
    stack->top = tmp;
    stack->count ++;
}

void Pop(LinkStack *stack, int *data)
{
    *data = stack->top->data;
    stack->top = stack->top->next;
}